// src/store/sidebarStore.ts

import { create } from 'zustand';

interface SidebarState {
  collapsed: boolean;
  setCollapsed: (collapsed: boolean) => void;
  toggleCollapsed: () => void;
}

const SIDEBAR_COLLAPSE_KEY = 'sidebar_collapsed';

function getInitialCollapsed() {
  const val = localStorage.getItem(SIDEBAR_COLLAPSE_KEY);
  return val === 'true';
}

export const useSidebarStore = create<SidebarState>((set) => ({
  collapsed: getInitialCollapsed(),
  setCollapsed: (collapsed) => {
    localStorage.setItem(SIDEBAR_COLLAPSE_KEY, String(collapsed));
    set({ collapsed });
  },
  toggleCollapsed: () => {
    set((state) => {
      const next = !state.collapsed;
      localStorage.setItem(SIDEBAR_COLLAPSE_KEY, String(next));
      return { collapsed: next };
    });
  },
}));